 package com.gmrz.uaf.crypto;
 
 import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.Properties;

import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import com.gmrz.util.Convert;
import com.gmrz.util.PropertiesSource;
 
 public class CryptoProcessorKeyGenerator
 {
//   public static void main(String[] args)
//     throws NoSuchAlgorithmException, IOException
//   {
//     String filename = "uaf.properties";
//     String propertyName = "uaf.default.external.encryption.key";
//     String keyType = "AES";
//     Integer size = Integer.valueOf(128);
//     FileInputStream fileInputStream = null;
//     FileOutputStream fileOutputStream = null;
//     BufferedReader fileReader = null;
//     BufferedWriter fileWriter = null;
//     InputStreamReader inputStream = null;
//     OutputStreamWriter outputStream = null;
//
//     Logger LOG = LogManager.getLogger(CryptoProcessorKeyGenerator.class);
//     if (args != null)
//       switch (args.length) {
//       case 4:
//         size = Integer.valueOf(Integer.parseInt(args[3]));
//       case 3:
//         keyType = args[2];
//       case 2:
//         propertyName = args[1];
//       case 1:
//         filename = args[0];
//       }
//     try {
//       PropertiesSource propSrc = new PropertiesSource(PropertiesSource.Type.FILE_PATH, filename);
//
//       Properties prop = propSrc.load();
//
//       String symKeyVal = prop.getProperty(propertyName);
//
//       if ((symKeyVal == null) || ("".equals(symKeyVal.trim())))
//       {
//         KeyGenerator generator = KeyGenerator.getInstance(keyType);
//         SecureRandom sRand = new SecureRandom();
//         generator.init(size.intValue(), sRand);
//
//         SecretKey symKey = generator.generateKey();
//
//         String symKeyBase64 = Convert.toBase64(symKey.getEncoded());
//
//         fileInputStream = new FileInputStream(filename);
//         inputStream = new InputStreamReader(fileInputStream, "UTF-8");
//         fileReader = new BufferedReader(inputStream);
//
//         String tmpName = filename + "_" + new Long(System.currentTimeMillis()).toString();
//
//         fileOutputStream = new FileOutputStream(tmpName);
//         outputStream = new OutputStreamWriter(fileOutputStream, "UTF-8");
//         fileWriter = new BufferedWriter(outputStream);
//
//         String line = fileReader.readLine();
//         boolean edited = false;
//         while (line != null) {
//           if (line.startsWith(propertyName)) {
//             edited = true;
//             fileWriter.write(propertyName + "=" + symKeyBase64);
//           } else {
//             fileWriter.write(line);
//           }
//           fileWriter.newLine();
//           line = fileReader.readLine();
//         }
//         if (!edited) {
//           fileWriter.write(propertyName + "=" + symKeyBase64);
//         }
//         Boolean deleteStatus = Boolean.valueOf(false);
//         File file = new File(filename);
//         if (file.exists()) {
//           deleteStatus = Boolean.valueOf(file.delete());
//         }
//         File tmpFile = new File(tmpName);
//
//         if (deleteStatus.booleanValue()) {
//           boolean renameSuccess = tmpFile.renameTo(file);
//           if (!renameSuccess)
//             LOG.error("\nCould not rename file " + tmpFile + "to File " + file);
//         }
//       }
//     }
//     finally
//     {
//       try
//       {
//         if (null != fileReader) {
//           fileReader.close();
//         }
//         if (null != fileWriter) {
//           fileWriter.close();
//         }
//         if (inputStream != null) {
//           inputStream.close();
//         }
//         if (outputStream != null) {
//           outputStream.close();
//         }
//         if (fileInputStream != null) {
//           fileInputStream.close();
//         }
//         if (fileOutputStream != null)
//           fileOutputStream.close();
//       }
//       catch (IOException e) {
//         LOG.error(e.getMessage());
//       }
//     }
//   }
 }
